<%!

float[] gain=null,loss=null;

float temp1,temp2;

float cal_rsi(float rs){
    return (100-(100/(1+rs)));    
}

float avg_first(float[] arr){
    float sum=0f;
    int i;
    for(i=0;i<arr.length;i++)
    {
        sum+=arr[i];
    }
    return sum/14;
}

float avg(float pre,float cur){
    return (((pre*13)+cur)/14);
}

%>

<%@page import="java.io.InputStreamReader"%>
<%@page import="java.net.URL"%>
<%@page import="java.util.LinkedList"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.io.FileReader"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.text.DateFormat"%>
<%@page import="java.util.Date"%>
<%@page import="java.io.File"%>
<%@page import="java.io.BufferedReader"%>
<%!
BufferedReader reader=null,reader_clone=null;
File file;
LinkedList<String> ll=new LinkedList<String>();
LinkedList<String> ll_date=new LinkedList<String>();
LinkedList<String> ll_final=new LinkedList<String>();
URL url;
%>

<%

     String com=request.getParameter("com");

     Date dt=new Date(System.currentTimeMillis());
                int day=dt.getDate();
                int month=dt.getMonth()+1;
                int year=dt.getYear()+1900;
              
                String u="http://ichart.yahoo.com/table.csv?s="+com+"&d="+(month-1)+"&e="+(day)+"&f="+year+"&a="+(month-1)+"&b="+(day)+"&c="+(year-1)+"&g=d&ignore=.csv";
                URL url=new URL(u);
                 reader = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"));
                //File f = new File("D:/project/src/java/quotes/table.csv");
                // reader = new BufferedReader(new FileReader(f));
                
                String s1,a=null,b=null,c=null,d=null,e=null;
                int k=0,g=0,t=0;
                Long l;
                Date dt1=null;
                
               DateFormat df;
               df=new SimpleDateFormat("yyyy-MM-dd");
               
               while((s1=reader.readLine())!=null){
                    
                             if(k==0){
                                 k++;
                                 continue;
                            }   
                             
                                 
                             
                        String[] tokens = s1.split(",");
                        
                        for(int i=0;i<tokens.length;i++)
                        {
                            if(i==0){
                                dt1=(Date)df.parse(tokens[i]);
                               // out.print(dt+"----");
                                l=dt1.getTime();
                                //out.print(l);
                                //a="[";
                                //a= a.concat(l.toString());
                                 ll_date.push(l.toString());
                            }
                            
                            if(i==4)
                                b=tokens[i];
                                                                                 
                            }
                       
                         ll.push(b);                   
                           
                        }
                    
                   
               temp1=Float.parseFloat(ll.poll().toString());
               loss=new float[20];
               gain=new float[20];
               k=0;
               //j=0;
               while(k!=14){
                   temp2=Float.parseFloat(ll.poll().toString());
                  ll_date.pop();
                   if(temp1>temp2){
                       loss[t++]=temp1-temp2;
                   }
                   if(temp2>=temp1){
                       gain[g++]=temp2-temp1;
                   }
                       
                   temp1=temp2;
                   k++;
               }
               
               float first_avg_gain=avg_first(gain);
               float first_avg_loss=avg_first(loss);
               ll_final.addLast("["+ll_date.poll()+","+cal_rsi(first_avg_gain/first_avg_loss)+"]");
              
              float pre_gain=first_avg_gain,pre_loss=first_avg_loss; 
              
              while(!ll.isEmpty()){
                  temp2=Float.parseFloat(ll.poll().toString());
                  if(temp2>=temp1){
                      ll_final.addLast("["+ll_date.poll()+","+cal_rsi(avg(pre_gain,temp2-temp1)/avg(pre_loss,0))+"]");
                      pre_gain=avg(pre_gain,temp2-temp1);
                      pre_loss=avg(pre_loss,0);
                      
                  }
                  if(temp1>temp2){
                      ll_final.addLast("["+ll_date.poll()+","+cal_rsi(avg(pre_gain,0)/avg(pre_loss,temp1-temp2))+"]");
                      pre_gain=avg(pre_gain,0);
                      pre_loss=avg(pre_loss,temp1-temp2);
                  }
                  
                 temp1=temp2; 
              }
              out.print(ll_final.toString());
                    reader.close();

%>